package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.channel.service.AnyidaiService;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.model.KwAydPush;
import com.meirengu.webview.model.KwAydPushLog;
import com.meirengu.webview.service.KwAydPushLogService;
import com.meirengu.webview.service.KwAydPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 平安投放
 *
 * @author Mao
 * @create 2017-12-15 12:11
 */
@Service
public class AnyidaiServiceImpl implements AnyidaiService {

    Logger logger = LoggerFactory.getLogger(AnyidaiServiceImpl.class);
    @Autowired
    private KwAydPushService kwAydPushService;
    @Autowired
    private KwAydPushLogService kwAydPushLogService;

    /**
     * @param
     * @return
     */

    public Integer applyLoan(String phone, BigDecimal amount, String name, String sex,
                             String birthday, String city, String house, String is_atom,
                             String career, String work_time, String policy, String accumulation_fund,
                             String social_security, String salary_modal, String credit_card, String month_income,
                             String car, String register_ip) throws Exception {

        StringBuffer url = new StringBuffer(ConfigUtil.getConfig("anyidai.url"));
        String channel = ConfigUtil.getConfig("anyidai.channel");
        String key = ConfigUtil.getConfig("anyidai.key");

        String now = String.valueOf(new Date().getTime());
        now = now.substring(0, 10);
        String sign = MD5Util.MD5(key + channel + now, "UTF-8");
        url.append("?channel=").append(channel).append("&sign=").append(sign).append("&time=").append(now);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", phone);
        jsonObject.put("amount", amount);
        jsonObject.put("name", name);
        jsonObject.put("sex", sex);
        jsonObject.put("birthday", birthday);
        jsonObject.put("city", city + "市");
        jsonObject.put("house", house);
        jsonObject.put("is_atom", is_atom);
        jsonObject.put("career", career);
        jsonObject.put("work_time", work_time);
        jsonObject.put("policy", policy);
        jsonObject.put("accumulation_fund", accumulation_fund);
        jsonObject.put("social_security", social_security);
        jsonObject.put("salary_modal", salary_modal);
        jsonObject.put("credit_card", credit_card);
        jsonObject.put("month_income", month_income);
        jsonObject.put("car", car);
        jsonObject.put("register_time", now);
        jsonObject.put("register_ip", register_ip);

        //接口调用信息表
        KwAydPush kwAydPush = new KwAydPush();
        kwAydPush.setInterfaceCode("ayd");
        kwAydPush.setInterfaceName("安易贷");
        kwAydPush.setMobile(phone);
        kwAydPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwAydPushLog kwAydPushLog = new KwAydPushLog();
        kwAydPushLog.setInterfaceCode("ayd");
        kwAydPushLog.setInterfaceName("安易贷");
        kwAydPushLog.setMobile(phone);
        kwAydPushLog.setSendUrl(url.toString());
        kwAydPushLog.setParams(jsonObject.toString());

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url.toString(), jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwAydPushLog.setBackUrl(hr.toString());
                kwAydPushLog.setHttpCode(200);
                kwAydPushLog.setHttpContent(hr.toString());
                logger.info("==AnyidaiServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("code");
                String rMsg = returnMsg.getString("info");
                String rData = returnMsg.getString("data");
                if ("0".equals(rCode) && "0".equals(rData)) {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwAydPush.setMessage("成功");
                    logger.info("==AnyidaiServiceImpl.applyLoan business success");
                } else if ("1".equals(rData)) {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                    kwAydPush.setMessage("rCode: " + rCode + "rMsg:用户已注册");
                    logger.warn("==AnyidaiServiceImpl.applyLoan business fail");
                } else {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwAydPush.setMessage("rCode: " + rCode + "rMsg:" + rMsg);
                    logger.warn("==AnyidaiServiceImpl.applyLoan business fail");
                }
            } else {
                kwAydPushLog.setBackUrl(hr.toString());
                kwAydPushLog.setHttpCode(code);
                kwAydPushLog.setHttpContent(hr.toString());
                logger.warn("==AnyidaiServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwAydPushLog.setHttpCode(408);
            kwAydPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwAydPushService.insert(kwAydPush);
            kwAydPushLog.setPushId(kwAydPush.getId());
            kwAydPushLogService.insert(kwAydPushLog);
            return kwAydPush.getCode();
        }
    }

    /**
     * @param
     * @return
     */

    public Integer applyLoan2(String phone, BigDecimal amount, String name, String sex,
                              String birthday, String city, String house, String is_atom,
                              String career, String work_time, String policy, String accumulation_fund,
                              String social_security, String salary_modal, String credit_card, String month_income,
                              String car, String register_ip) throws Exception {

        StringBuffer url = new StringBuffer(ConfigUtil.getConfig("anyidai.url2"));
        String channel = ConfigUtil.getConfig("anyidai.channel2");
        String key = ConfigUtil.getConfig("anyidai.key2");

        String now = String.valueOf(new Date().getTime());
        now = now.substring(0, 10);
        String sign = MD5Util.MD5(key + channel + now, "UTF-8");
        url.append("?channel=").append(channel).append("&sign=").append(sign).append("&time=").append(now);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", phone);
        jsonObject.put("amount", amount);
        jsonObject.put("name", name);
        jsonObject.put("sex", sex);
        jsonObject.put("birthday", birthday);
        jsonObject.put("city", city + "市");
        jsonObject.put("house", house);
        jsonObject.put("is_atom", is_atom);
        jsonObject.put("career", career);
        jsonObject.put("work_time", work_time);
        jsonObject.put("policy", policy);
        jsonObject.put("accumulation_fund", accumulation_fund);
        jsonObject.put("social_security", social_security);
        jsonObject.put("salary_modal", salary_modal);
        jsonObject.put("credit_card", credit_card);
        jsonObject.put("month_income", month_income);
        jsonObject.put("car", car);
        jsonObject.put("register_time", now);
        jsonObject.put("register_ip", register_ip);


        //接口调用信息表
        KwAydPush kwAydPush = new KwAydPush();
        kwAydPush.setInterfaceCode("ayd2");
        kwAydPush.setInterfaceName("安易贷2");
        kwAydPush.setMobile(phone);
        kwAydPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwAydPushLog kwAydPushLog = new KwAydPushLog();
        kwAydPushLog.setInterfaceCode("ayd2");
        kwAydPushLog.setInterfaceName("安易贷2");
        kwAydPushLog.setMobile(phone);
        kwAydPushLog.setSendUrl(url.toString());
        kwAydPushLog.setParams(jsonObject.toString());

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url.toString(), jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwAydPushLog.setBackUrl(hr.toString());
                kwAydPushLog.setHttpCode(200);
                kwAydPushLog.setHttpContent(hr.toString());

                logger.info("==AnyidaiServiceImpl.applyLoan2 request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("code");
                String rMsg = returnMsg.getString("info");
                String rData = returnMsg.getString("data");
                if ("0".equals(rCode) && "0".equals(rData)) {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwAydPush.setMessage("成功");
                    logger.info("==AnyidaiServiceImpl.applyLoan2 business success");
                } else if ("1".equals(rData)) {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                    kwAydPush.setMessage("rCode: " + rCode + "rMsg:用户已注册");
                    logger.warn("==AnyidaiServiceImpl.applyLoan2 business fail");
                } else {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwAydPush.setMessage("rCode: " + rCode + "rMsg:" + rMsg);
                    logger.warn("==AnyidaiServiceImpl.applyLoan2 business fail");
                }
            } else {
                kwAydPushLog.setBackUrl(hr.toString());
                kwAydPushLog.setHttpCode(code);
                kwAydPushLog.setHttpContent(hr.toString());
                logger.warn("==AnyidaiServiceImpl.applyLoan2 request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwAydPushLog.setHttpCode(408);
            kwAydPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwAydPushService.insert(kwAydPush);
            kwAydPushLog.setPushId(kwAydPush.getId());
            kwAydPushLogService.insert(kwAydPushLog);
            return kwAydPush.getCode();
        }
    }


    /**
     * @param
     * @return
     */

    public Integer applyLoan3(String phone, BigDecimal amount, String name, String sex,
                              String birthday, String city, String house, String is_atom,
                              String career, String work_time, String policy, String accumulation_fund,
                              String social_security, String salary_modal, String credit_card, String month_income,
                              String car, String register_ip) throws Exception {

        StringBuffer url = new StringBuffer(ConfigUtil.getConfig("anyidai.url3"));
        String channel = ConfigUtil.getConfig("anyidai.channel3");
        String key = ConfigUtil.getConfig("anyidai.key3");

        String now = String.valueOf(new Date().getTime());
        now = now.substring(0, 10);
        String sign = MD5Util.MD5(key + channel + now, "UTF-8");
        url.append("?channel=").append(channel).append("&sign=").append(sign).append("&time=").append(now);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("phone", phone);
        jsonObject.put("amount", amount);
        jsonObject.put("name", name);
        jsonObject.put("sex", sex);
        jsonObject.put("birthday", birthday);
        jsonObject.put("city", city + "市");
        jsonObject.put("house", house);
        jsonObject.put("is_atom", is_atom);
        jsonObject.put("career", career);
        jsonObject.put("work_time", work_time);
        jsonObject.put("policy", policy);
        jsonObject.put("accumulation_fund", accumulation_fund);
        jsonObject.put("social_security", social_security);
        jsonObject.put("salary_modal", salary_modal);
        jsonObject.put("credit_card", credit_card);
        jsonObject.put("month_income", month_income);
        jsonObject.put("car", car);
        jsonObject.put("register_time", now);
        jsonObject.put("register_ip", register_ip);

        //接口调用信息表
        KwAydPush kwAydPush = new KwAydPush();
        kwAydPush.setInterfaceCode("ayd3");
        kwAydPush.setInterfaceName("安易贷3");
        kwAydPush.setMobile(phone);
        kwAydPush.setCode(Constants.BUSINESS_STATUS_FAIL);

        KwAydPushLog kwAydPushLog = new KwAydPushLog();
        kwAydPushLog.setInterfaceCode("ayd3");
        kwAydPushLog.setInterfaceName("安易贷3");
        kwAydPushLog.setMobile(phone);
        kwAydPushLog.setSendUrl(url.toString());
        kwAydPushLog.setParams(jsonObject.toString());

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPost(url.toString(), jsonObject.toString());
            int code = hr.getStatusCode();
            if (code == 200) {
                kwAydPushLog.setBackUrl(hr.toString());
                kwAydPushLog.setHttpCode(200);
                kwAydPushLog.setHttpContent(hr.toString());

                logger.info("==AnyidaiServiceImpl.applyLoan3 request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("code");
                String rMsg = returnMsg.getString("info");
                String rData = returnMsg.getString("data");
                if ("0".equals(rCode) && "0".equals(rData)) {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwAydPush.setMessage("成功");
                    logger.info("==AnyidaiServiceImpl.applyLoan3 business success");
                } else if ("1".equals(rData)) {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_REPEAT);
                    kwAydPush.setMessage("rCode: " + rCode + "rMsg:用户已注册");
                    logger.warn("==AnyidaiServiceImpl.applyLoan3 business fail");
                } else {
                    kwAydPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwAydPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    logger.warn("==AnyidaiServiceImpl.applyLoan3 business fail");
                }
            } else {
                kwAydPushLog.setBackUrl(hr.toString());
                kwAydPushLog.setHttpCode(code);
                kwAydPushLog.setHttpContent(hr.toString());
                logger.warn("==AnyidaiServiceImpl.applyLoan3 request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwAydPushLog.setHttpCode(408);
            kwAydPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            kwAydPushService.insert(kwAydPush);
            kwAydPushLog.setPushId(kwAydPush.getId());
            kwAydPushLogService.insert(kwAydPushLog);
            return kwAydPush.getCode();
        }
    }



}
